﻿$(function () {
    var Province, City, Area, OtherAddr;
    //创建Map实例
    var map = new BMap.Map("allmap");
    var location = $("#MapPoint").val();
    if (location != "0.000000,0.000000") {
        map.centerAndZoom(new BMap.Point(location.split(',')[0], location.split(',')[1]), 16);
        theLocation(location.split(',')[0], location.split(',')[1]);
    } else {
        LoadMap();
    }
    // 添加带有定位的导航控件
    var navigationControl = new BMap.NavigationControl({
        // 靠左上角位置
        anchor: BMAP_ANCHOR_TOP_LEFT,
        // LARGE类型  	
        /*缩放控件type有四种类型:BMAP_NAVIGATION_CONTROL_SMALL：仅包含平移和缩放按钮；BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮；BMAP_NAVIGATI*/
        type: BMAP_NAVIGATION_CONTROL_LARGE
        //// 启用显示定位
        //enableGeolocation: true
    });
    map.addControl(navigationControl);

    //map.centerAndZoom(point, 8);
    //map.clearOverlays();    //清除地图上所有覆盖物
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    //事件监听
    //map.addEventListener("dragend", function () {
    //    var centerPoint = map.getCenter();
    //    $("#MapPoint").val(centerPoint.lat + "," + centerPoint.lng);
    //});

    //单击获取点击的经纬度
    map.addEventListener("click", function (e) {
        theLocation(e.point.lng, e.point.lat);
        $("#MapPoint").val(e.point.lng + "," + e.point.lat);

    });

    $("#get_point_at_map").click(function () {

        Province = $("#F_ProvinceKid option:selected").text();//省
        City = $("#F_CityKid option:selected").text();//城市
        Area = $("#F_AreaKid option:selected").text();//城市
        OtherAddr = $("#F_Address").val();
        if (OtherAddr.length > 0)
        {
            f_Map(Province, City + OtherAddr,16);
        } else {
            f_Map(Province, City + Area,12);
        }
      
    });

    $('#F_ProvinceKid').change(function () {

        Province = $("#F_ProvinceKid option:selected").text();//省
        f_Map(Province, Province,8);
    });

    $('#F_CityKid').change(function () {
        Province = $("#F_ProvinceKid option:selected").text();//省
        City = $("#F_CityKid option:selected").text();//城市

        f_Map(Province, City,10);
    });

    $('#F_AreaKid').change(function () {
        Province = $("#F_ProvinceKid option:selected").text();//省
        City = $("#F_CityKid option:selected").text();//城市
        Area = $("#F_AreaKid option:selected").text();//城市
        f_Map(Province, City + Area,12);
    });

    //$('#F_Address').change(function () {
    //    Province = $("#F_ProvinceKid option:selected").text();//省
    //    City = $("#F_CityKid option:selected").text();//城市
    //    Area = $("#F_AreaKid option:selected").text();//城市
    //    OtherAddr = $("#F_Address").val();

    //    if (OtherAddr.length > 0) {
    //        f_Map(Province, City + OtherAddr, 16);
    //        $("#AreaDiv").hide();
    //    } else {
    //        f_Map(Province, City + Area, 12);
    //        $("#AreaDiv").show();
    //    }
    //});

    ///页面载入的时候,载入地图
    function LoadMap() {
        Province = $("#F_ProvinceKid option:selected").text();//省
        City = $("#F_CityKid option:selected").text();//城市
        Area = $("#F_AreaKid option:selected").text();//城市
        OtherAddr = $("#F_Address").val();

        if (OtherAddr.length > 0) {
            $("#AreaDiv").hide();
            //$("#F_AreaKid").empty();
            //$("#F_AreaKid").attr("disabled", true);
            f_Map(Province, City + OtherAddr, 16);
        } else {
            $("#AreaDiv").show();
            f_Map(Province, City + Area , 12);
        }
    }


    //根据地址解析，返回坐标
    function f_Map(Province, OtherAddr, multiple) {
        map.clearOverlays();
        // 创建地址解析器实例
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上,并调整地图视野
        myGeo.getPoint(OtherAddr, function (point) {
            if (point) {

                $("#MapPoint").val(point.lng + "," + point.lat);
                map.centerAndZoom(point, multiple);
                map.addOverlay(new BMap.Marker(point));
            }
        }, Province);
    }


    // 根据经纬度,返回位置
    function theLocation(longitude, latitude) {
        map.clearOverlays();
        var new_point = new BMap.Point(longitude, latitude);
        var marker = new BMap.Marker(new_point);  // 创建标注
        map.addOverlay(marker);              // 将标注添加到地图中
        map.panTo(new_point);
    }

});

function f_AddressClick() {
    //var Province = $("#F_ProvinceKid option:selected").text();//省
    //var City = $("#F_CityKid option:selected").text();//城市
    //var Area = $("#F_AreaKid option:selected").text();//城市
    var OtherAddr = $("#F_Address").val();
    if (OtherAddr.length > 0) {
        //f_Map(Province, City + OtherAddr, 16);
        $("#AreaDiv").hide();
    } else {
        //f_Map(Province, City + Area, 12);
        $("#AreaDiv").show();
    }
}

